﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SmartHotel.Dto;
using SmartHotel.House.Dto;
using SmartHotel.Menu;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace SmartHotel.House
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class SmartHotelAppHouseService : ApplicationService, ISmartHotelAppHouseService
    {
        private readonly IRepository<SmartHotel_HouseTable, int> repository;

        public SmartHotelAppHouseService(IRepository<SmartHotel_HouseTable, int> repository)
        {
            this.repository = repository;
        }
        /// <summary>
        /// 添加房型
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateUpdateHouse>> CreateHouseAsync(CreateUpdateHouse model)
        {
            model.AveragePrice = model.HousePrice;
            var date = await repository.InsertAsync(ObjectMapper.Map<CreateUpdateHouse, SmartHotel_HouseTable>(model));
            return new ResultDto<CreateUpdateHouse> { Code = Result.Success, Data = date, Message = "添加成功" };
        }
        /// <summary>
        /// 删除房型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<int>> DeleteHouseAsync(int id)
        {
            var date = await repository.FindAsync(id);
            await repository.DeleteAsync(date);
            return new ResultDto<int> { Code = Result.Success, Data = date, Message = "删除成功" };
        }
        /// <summary>
        /// 根据id获取房型信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<UserRoleDto>> GetHouseFindId(int id)
        {
            var date = await repository.GetAsync(id);
            var dto = ObjectMapper.Map<SmartHotel_HouseTable, HouseDto>(date);
            return new ResultDto<UserRoleDto> { Code = Result.Success, Data = dto, Message = "查询成功" };
        }
        /// <summary>
        /// 房型信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="HouseName"></param>
        /// <returns></returns>
        public async Task<ResultDto<HouseDto>> GetHouseAsync()
        {
            var data = await repository.GetListAsync();
            var dto = ObjectMapper.Map<List<SmartHotel_HouseTable>, List<HouseDto>>(data);
            return new ResultDto<HouseDto> { Code = Result.Success, Data = dto, Message = "显示成功" };
        }

        /// <summary>
        /// 分页显示房型信息+模糊查询房型名称
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="HouseName"></param>
        /// <returns></returns>
        public async Task<PageDto<HouseDto>> GetUserHouseAsync(int pageIndex, int pageSize, string HouseName)
        {
            var count = await repository.CountAsync();
            var data = await repository.GetListAsync();
            data = data.WhereIf(!string.IsNullOrEmpty(HouseName), x => x.HouseName.Contains(HouseName)).ToList();
            //data=data.Where(x=>x.IsDeleted==)
            var list = data.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            list = list.OrderByDescending(x => x.GouseRank).ToList();
            var dto = ObjectMapper.Map<List<SmartHotel_HouseTable>, List<HouseDto>>(list);
            return new PageDto<HouseDto> { Code = Result.Success, Data = dto, Count = count, Message = "显示成功" };
        }
        /// <summary>
        /// 修改房型信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateUpdateHouse>> UpdateHouseAsync(CreateUpdateHouse model)
        {
            var list = await repository.GetAsync(model.Id);
            var data = ObjectMapper.Map(model, list);
            await repository.UpdateAsync(data);
            var dto = ObjectMapper.Map<SmartHotel_HouseTable, CreateUpdateHouse>(data);
            return new ResultDto<CreateUpdateHouse> { Code = Result.Success,Data=dto, Message = "修改成功" };
        }
        /// <summary>
        /// 修改房型自定义房价（均价）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultDto<int>> UpdateAVHouseAsync(int id,decimal CustomPrice)
        {
            var list = await repository.GetAsync(id);
            list.AveragePrice = CustomPrice;
            //var data = ObjectMapper.Map<SmartHotel_HouseTable,CreateUpdateHouse >(list);
            await repository.UpdateAsync(list);
            var dto = ObjectMapper.Map<SmartHotel_HouseTable, CreateUpdateHouse>(list);
            return new ResultDto<int> { Code = Result.Success, Data = dto, Message = "修改成功" };
        }
        /// <summary>
        /// 添加房型图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public List<IFormFile> PostUpload(List<IFormFile> file)
        {
            foreach (var item in file.ToList())
            {
                string saveFile = Directory.GetCurrentDirectory() + @"/wwwroot/File/" + item.FileName;
                using (FileStream fs = new FileStream(saveFile, FileMode.Create))
                {
                    item.CopyTo(fs);
                    fs.Flush();
                }
            }
            
            return file.ToList();
        }
        /// <summary>
        /// 房型的批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<ResultDto<int>> PostDeleteBatchHouse(int[] ids)
        {
            await repository.DeleteManyAsync(ids);
            return new ResultDto<int> { Code= Result.Success,Message="批量删除成功" };
        }
        /// <summary>
        /// 房型排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="houserank"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultDto<int>> PutHousenum(int id, int houserank)
        {
            var list = await repository.GetAsync(id);
            list.GouseRank= houserank;
            await repository.UpdateAsync(list);
            return new ResultDto<int> { Code = Result.Success };
        }
    }
}
